<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class User extends MX_Controller {

	public function __construct() {
		parent::__construct();

		$this->template
		->set_layout('default') // application/views/layouts/two_col.php
		->set_theme('admin'); // application/views/layouts/two_col.php
		$this->load->model(array('admin_member_model','admin_settings_model','admin_location_model'));
		$this->admin_member_model->check_admin_login();

		$this->template->set_partial('nav_bar', 'layouts/nav_bar');
		$this->template->set_partial('side_menu', 'layouts/side_menu');
		$this->template->set_partial('footer','footer/footer');


	}
	
	
	
	public function all($status = 'all'){

		if(isset($type) && $type == 'active'){
			$sql = "select * from member where type != 2 and status = 51";
		}
		elseif(isset($type) && $type == 'inactive'){
			$sql = "select * from member where type != 2 and status = 1";
		}else{
			$sql = "select * from member where type != 2";
		}

		$result = $this->db->query($sql)->result();

		$data['member'] = $result;

		
		
		
		$this->template->build('member/report',$data);
	}

	public function add(){

		$data['mode'] = "ADD";
		
		$data['region'] = $this->location_model->getLocation(0,99999);
		
		$this->template->build('member/add',$data);
	}
	
	function submit()
	{
		$this->form_validation->set_rules('name','User Name','xss_clean|required');
		$this->form_validation->set_rules('email','Email','xss_clean|required|valid_email');
		$this->form_validation->set_rules('gender','Gender','xss_clean|required');
		$this->form_validation->set_rules('type','User Type','xss_clean|required');
		$this->form_validation->set_rules('region','Region','xss_clean|required');
		$this->form_validation->set_rules('city','City','xss_clean|required');
		$this->form_validation->set_rules('password','Password','xss_clean|required');
		$this->form_validation->set_rules('retype_password','Retype Password','xss_clean|matches[password]');
		
		
		if($this->form_validation->run() == TRUE){
			
			$post_data = array(
					
					'name' => $this->input->post('name'),
					'type' => $this->input->post('type'),
					'email' => $this->input->post('email'),
					'gender' => $this->input->post('gender'),
					'region' => $this->input->post('region'),
					'city' => $this->input->post('city'),
					'password' => $this->input->post('password')
					
					);
			
			$this->db->insert('member',$post_data);
			$msgs = "<div class='alert alert-info'> User Add Successfully</div>";
			$this->session->set_flashdata('message', $msgs);
			redirect('secure/member/user/add');
			
			
		}else{
			
			$this->add();
		}
		
		
	}
	
	function edit($id){
		
		$data['user'] = getPK('member',$id);
		
		$data['mode'] = "EDIT";
		
		$data['region'] = $this->location_model->getLocation(0,99999);
		
		$data['city'] = $this->location_model->getCities($data['user']['region'],99999);
		
		//var_dump($data['city']);die();
		
		$this->template->build('member/add',$data);
		
	}
	
	function update(){
		
		$id = $this->input->post('user_id');
		
		$this->form_validation->set_rules('name','User Name','xss_clean|required');
		$this->form_validation->set_rules('email','Email','xss_clean|required|valid_email');
		$this->form_validation->set_rules('gender','Gender','xss_clean|required');
		$this->form_validation->set_rules('type','User Type','xss_clean|required');
		$this->form_validation->set_rules('region','Region','xss_clean|required');
		$this->form_validation->set_rules('city','City','xss_clean|required');

		
		if($this->form_validation->run() == TRUE){
				
			$post_data = array(
						
					'name' => $this->input->post('name'),
					'type' => $this->input->post('type'),
					'email' => $this->input->post('email'),
					'gender' => $this->input->post('gender'),
					'region' => $this->input->post('region'),
					'city' => $this->input->post('city'),

			);
			
		//	var_dump($post_data);die();
				
			$this->db->where('id',$id);
			$this->db->update('member',$post_data);
			$msgs = "<div class='alert alert-success'> User Update Successfully</div>";
			$this->session->set_flashdata('message', $msgs);
			redirect('secure/member/user/edit/'.$id);
				
				
		}else{
				
			$this->edit($id);
		}
		
	}
	
	function delete($id){
		$sql = "Delete from member where id='$id'";
		$this->db->query($sql);
		
		redirect('secure/member/user/all');
		//$this->all();
	}
}